package com.cse.mvc.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cse.engine.EngineFactory;
import com.cse.engine.EngineInfo;
import com.cse.engine.subEngine.Engine;
import com.cse.mvc.models.Result;
import com.cse.services.SearchService;
import com.cse.services.impl.SearchServiceImpl;
import com.cse.utils.AsyncCommunicationsInterface;
import com.cse.utils.Util;

@Controller
public class IndexController {

	private List<EngineInfo> engineInfo;
	private String key = null;

	/**
	 * 初始页面
	 * 
	 * @return
	 */
	@RequestMapping("/")
	public ModelAndView index() {
		ModelAndView modelAndView = new ModelAndView(Util.DEFAULT_JSP_NAME);
		// 获取所有引擎信息
		// 引擎信息存储在 EngineFactory 中
		EngineFactory engineFactory = EngineFactory.getEngineFactory();
		engineInfo = engineFactory.getEngineList();

		// 将数据绑定到 modelAndView 上
		modelAndView.addObject(Util.ENGINE_INFO_ATT_NAME, engineInfo);

		return modelAndView;
	}

	/**
	 * 搜索页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/search")
	public ModelAndView search(HttpServletRequest request,
			HttpServletResponse response) {
		List<Result> result = search4Result(request);

		ModelAndView modelAndView = new ModelAndView(
				Util.SEARCH_RESULT_JSP_NAME);
		modelAndView.addObject(Util.SEARCH_RESULT_ATT_NAME, result);
		modelAndView.addObject(Util.KEY_NAME, key);
		return modelAndView;
	}
	
	@RequestMapping(value = "/s")
	public ModelAndView s(HttpServletRequest request,
			HttpServletResponse response){
		List<Result> result = search4Result(request);
		String json = null;
		try {
			json = new ObjectMapper().writeValueAsString(result);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return new ModelAndView("json").addObject("json", json);
	}

	private List<Result> search4Result(HttpServletRequest request) {
		// 获取用户所选中的搜索引擎
		List<String> engineNames = new ArrayList<String>();
		for (int i = 0; i < engineInfo.size(); i++) {
			String checkboxParameter = request.getParameter(engineInfo.get(i)
					.getName());
			if (null != checkboxParameter
					&& checkboxParameter
							.equalsIgnoreCase(Util.CHECK_BOX_CHECKED_TAG)) {
				engineNames.add(engineInfo.get(i).getName());
			}
		}

		// 测试使用代码，当用户没有选择搜索引擎时，自动选择第一个引擎
		if(engineNames.size() == 0){
			engineNames.add(engineInfo.get(1).getName());
		}
		
		SearchService searchService = new SearchServiceImpl();
		key = request.getParameter(Util.KEY_NAME);
		List<Result> result = searchService.search(key, engineNames);
		return result;
	}

}
